var browser;
$(function () {
    window.setInterval(function(){
        slideleft();
    }, 3000);
    function slideleft() {
        $('#parent>div:first').animate({width:'0px'},2000,callback);
    }
    function callback() {
        $('#parent>div:last').after($('#parent>div:first'));
        $('#parent>div:last').css('width', '398px');
    }


    browser = {versions : function() {
        var u = navigator.userAgent, app = navigator.appVersion;
        return {//移动终端浏览器版本信息
            trident : u.indexOf('Trident') > -1, //IE内核
            presto : u.indexOf('Presto') > -1, //opera内核
            webKit : u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
            gecko : u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
            mobile : !!u.match(/AppleWebKit.*Mobile/i) || !!u.match(/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/), //是否为移动终端
            ios : !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
            android : u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或者uc浏览器
            iPhone : u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1, //是否为iPhone或者QQHD浏览器
            iPad : u.indexOf('iPad') > -1, //是否iPad
            webApp : u.indexOf('Safari') == -1
            //是否web应该程序，没有头部与底部
        };
    }(),language : (navigator.browserLanguage || navigator.language).toLowerCase()
    };

    if (browser.versions.iPhone
        || browser.versions.iPad
        || browser.versions.ios) {
        if(isWeiXin()) {
            alert("友情提示：点击右上角,使用safari打开");
        }
    }
    var regphone = /^1[34578]\d{9}$/;
    var regcode = /^\d{6}$/;
    var regnum = /^[0-9]*$/;
    $("#yzcode").click(function () {
        var yzcode = $("#yzcode").html();
        if (yzcode != '获取验证码' && yzcode != '再次获取')
        var phone = $("#phone").val();
        if (phone == undefined || phone == null || phone == '') {
            $("#dialogbtn").show();
            $("#dialogmsg").html("请输入手机号")
            setTimeout("dialoghide();",3000);//1000为1秒钟
            $("#yzcode").html('获取验证码');
            return;
        } else if (!regphone.test(phone)) {
            $("#dialogbtn").show();
            $("#dialogmsg").html("请输入正确格式的手机号")
            setTimeout("dialoghide();",3000);//1000为1秒钟
            $("#yzcode").html('获取验证码');
            return;
        }
        var url = "usercontroller/getphonecode.do";
        var json = "{phone:" + phone + ",type:0,tag:hq}";
        $.ajax({
            url: url,
            type: 'post',
            dataType: "json",
            data: {
                "json": json
            },
            async: false,
            success: function (data) {
                var code = data.code;
                if (code == 20000) {
                    $("#dialogbtn").show();
                    $("#dialogmsg").html("获取成功")
                    setTimeout("dialoghide();",3000);//1000为1秒钟
                    $("#sec").val(60);
                } else {
                    var message = data.message;
                    $("#dialogbtn").show();
                    $("#dialogmsg").html(message)
                    setTimeout("dialoghide();",3000);//1000为1秒钟
                    $("#yzcode").html('获取验证码');
                }
            },
            error: function () {
                $("#yzcode").html('获取验证码');
                setTimeout("dialoghide();",3000);//1000为1秒钟
            }
        });
    });

    $("#code").bind({
        blur: function () {
            var code = $("#code").val();
            if (code == undefined || code == null || code == "") {
                $("#dialogbtn").show();
                $("#dialogmsg").html("请输入验证码")
                setTimeout("dialoghide();",3000);//1000为1秒钟
                return;
            } else if (!regcode.test(code)) {
                $("#dialogbtn").show();
                $("#dialogmsg").html("请输入正确格式的验证码")
                setTimeout("dialoghide();",3000);//1000为1秒钟
                return;
            }
            var phone = $("#phone").val();
            if (phone == undefined || phone == null || phone == '') {
                $("#dialogbtn").show();
                $("#dialogmsg").html("请输入手机号")
                setTimeout("dialoghide();",3000);//1000为1秒钟
                return;
            } else if (!regphone.test(phone)) {
                $("#dialogbtn").show();
                $("#dialogmsg").html("请输入正确格式的手机号")
                setTimeout("dialoghide();",3000);//1000为1秒钟
                return;
            }
            var url = "usercontroller/getphonecode.do";
            var json = "{phone:" + phone + ",type:0,tag:yz,code=" + code + "}";
            $.ajax({
                url: url,
                type: 'post',
                dataType: "json",
                data: {
                    "json": json
                },
                async: false,
                success: function (data) {
                    var code = data.code;
                    if (code == 20000) {
                        var phonecode = data.data.phonecode;
                        $("#phonecode").val(phonecode);
                        return;
                    } else {
                        var message = data.message;
                        $("#dialogbtn").show();
                        $("#dialogmsg").html(message)
                        setTimeout("dialoghide();",3000);//1000为1秒钟
                    }
                },
                error: function () {

                }
            });
        }
    });

    $("#register").click(function () {
        var register = $("#register").html();
        if(register != '注册')
            return;
        else
            $("#register").html('注册中，请稍后...');
        var phone = $("#phone").val();
        var invitation = $("#invitcode").val();
        var password = $("#password").val();
        var phonecode = $("#phonecode").val();
        var regpassword = $("#regpassword").val();
        if (phone == undefined || phone == null || phone == '') {
            $("#dialogbtn").show();
            $("#dialogmsg").html("请输入手机号");
            $("#register").html('注册');
            setTimeout("dialoghide();",3000);//1000为1秒钟
            return;
        } else if (!regphone.test(phone)) {
            $("#dialogbtn").show();
            $("#dialogmsg").html("请输入正确格式的手机号");
            $("#register").html('注册');
            setTimeout("dialoghide();",3000);//1000为1秒钟
            return;
        }
        if (password == undefined || password == null || password == "") {
            $("#dialogbtn").show();
            $("#dialogmsg").html("请输入密码");
            $("#register").html('注册');
            setTimeout("dialoghide();",3000);//1000为1秒钟
            return;
        } else if (regpassword == undefined || regpassword == null || regpassword == "") {
            $("#dialogbtn").show();
            $("#dialogmsg").html("确认密码不能为空");
            $("#register").html('注册');
            setTimeout("dialoghide();",3000);//1000为1秒钟
            return;
        } else if(regnum.test(password)) {
            $("#dialogbtn").show();
            $("#dialogmsg").html("密码不能为纯数字");
            $("#register").html('注册');
            setTimeout("dialoghide();",3000);//1000为1秒钟
            return;
        } else if(regnum.test(regpassword)) {
            $("#dialogbtn").show();
            $("#dialogmsg").html("确认密码不能为纯数字");
            $("#register").html('注册');
            setTimeout("dialoghide();",3000);//1000为1秒钟
            return;
        } else if (regpassword != password) {
            $("#dialogbtn").show();
            $("#dialogmsg").html("两次密码输入不一致");
            $("#register").html('注册');
            setTimeout("dialoghide();",3000);//1000为1秒钟
            return;
        }
        password = hex_md5(password);
        var url = "usercontroller/register.do";
        var json = "{phone:" + phone + ",invitation:" + invitation + ",password:" + password + ",phonecode:" + phonecode + "}";
        $.ajax({
            url: url,
            type: 'post',
            dataType: "json",
            data: {
                "json": json
            },
            async: false,
            success: function (data) {
                var code = data.code;
                if (code == 20000) {
                    $("#dialogbtn").show();
                    $("#dialogmsg").html("注册成功")
                    setTimeout("dialoghide();",3000);//1000为1秒钟
                    $("#register").html('注册成功');
                    if (browser.versions.iPhone
                        || browser.versions.iPad
                        || browser.versions.ios) {
                        window.location.href = "https://itunes.apple.com/us/app/qi-xing-yun.life/id1151665439?mt=8";
                    }
                    if (browser.versions.android) {
                        window.location.href = "http://app.qq.com/#id=detail&appid=1105600529";
                    }
                    return;
                } else {
                    var message = data.message;
                    $("#dialogbtn").show();
                    $("#dialogmsg").html(message)
                    setTimeout("dialoghide();",3000);//1000为1秒钟
                    $("#register").html('注册');
                    alert(message);
                }
            },
            error: function () {

            }
        });
    });
});

setInterval("updateYzcodeSec()", 1000);//1000为1秒钟

function updateYzcodeSec() {
    var sec = $("#sec").val();
    if (sec == undefined || sec == null || sec == "")
        return;
    sec = sec * 1 - 1;
    if (sec == 0) {
        $("#sec").val("");
        $("#yzcode").html('<span style="line-height: 2rem">再次获取</span>');
    }
    else {
        $("#sec").val(sec);
        $("#yzcode").html('<span style="line-height: 2rem">'+sec+'秒后再次获取</span>');
    }
}

function  dialoghide() {
    $("#dialogbtn").hide();
}

function isWeiXin(){
    var ua = window.navigator.userAgent.toLowerCase();
    if(ua.match(/MicroMessenger/i) == 'micromessenger'){
        return true;
    }else{
        return false;
    }
}